﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;


namespace CTEA_INDIA
{
    public partial class UserProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;

            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("Default.aspx");
            }

            if (!IsPostBack)
            {
                GetUserMasterProfile();

                GetLoginDetails();
               
            }

        }

        public void GetLoginDetails()
        {
            int userid = Convert.ToInt32(Session["UserID"]);
            int login = Convert.ToInt32(Session["login"]);
            string[] Details = new string[2];
            Details = BAL.Common.GetLoginDetails(userid, login);
            spnUsername.Visible = true;
            spnLastUsedDate.Visible = true;
            spnUsername.InnerHtml = "Welcome  " + "<span class=\"spnWelcomeName\">" + Details[0] + "</span> ";
            spnLastUsedDate.InnerHtml = " Last used: " + Details[1];
        }
        public void GetUserMasterProfile()
        {
            int UserId = Convert.ToInt32(Session["UserID"]);
            string[] details = BAL.Customers.GetUserMasterProfile(UserId);

            lblEmail.Text=details[0];
            txtName.Text = details[1];
            txtAddress.Text = details[2];
            txtLocation.Text = details[3];
            txtWebsite.Text = details[4];
            txtcno.Text = details[5];
            txtContactP_Name.Text = details[6];
            txtContactP_EmailID.Text = details[7];
            txtPers_MobileNo.Text = details[8];

            Disable();
        }


        public void Disable()
        {
            txtName.Enabled = false;
            txtAddress.Enabled = false;
            txtLocation.Enabled = false;
            txtWebsite.Enabled = false;
            txtcno.Enabled = false;
            txtContactP_Name.Enabled = false;
            txtContactP_EmailID.Enabled = false;
            txtPers_MobileNo.Enabled = false;

            btnCancel.Visible = false;
            btnEdit.Visible = true;
            btnUpdate.Visible = false;
        }

        public void Enable()
        {
            txtName.Enabled = true ;
            txtAddress.Enabled = true;
            txtLocation.Enabled = true;
            txtWebsite.Enabled = true;
            txtcno.Enabled = true;
            txtContactP_Name.Enabled = true;
            txtContactP_EmailID.Enabled = true;
            txtPers_MobileNo.Enabled = true;

            btnCancel.Visible = true;
            btnUpdate.Visible = true;


        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int UserId = Convert.ToInt32(Session["UserID"]);
            string name = txtName.Text;
            string address = txtAddress.Text;
            string loc = txtLocation.Text;
            string phno = txtcno.Text;
            string website = txtWebsite.Text;
            string cpname = txtContactP_Name.Text;
            string cpmob = txtPers_MobileNo.Text;
            string cpemail = txtContactP_EmailID.Text;

            BAL.Customers.UpdateUserMaster(name, address, loc, phno, website, cpname, cpmob, cpemail, UserId);
            idspanupdateMsg.Style.Add("display", "block");
            idspanupdateMsg.InnerHtml = "Your Profile Updated Successfully";

            Disable();

          //  updatemessage.InnerHtml = ("Your Account Updated Successfully...!!");

            GetLoginDetails();
            btnEdit.Visible = true;


        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            btnEdit.Visible = false;
            Enable();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            btnCancel.Visible = false;
            Disable();
        }
    }
}